<?php
include('./init.php');
include('./helper.php');
$files=scandir('./template');
isset($submit) or $submit='';
//string,callback,regexp
if($submit){
	$all_rules=$db->mselect("select * from rules order by id asc");
	$fds=explode(',',trim($fields,','));
	$big=array();
	foreach($fds as $fd){
		$result=$text;
		
		foreach($all_rules as $rule){
			if(in_array($rule['id'],$rules)){
				@list($kind,$ext)=explode(':',$rule['kind']);
				
				
				switch($kind){
					case 'string':
					$res=array();
					
					$arr=array();
					if($rule['times']>1){
						foreach(range(1,$rule['times']) as $i){
							if($rule['dst']=='$$$0'){
								$arr[$rule['src']]=$fd.'_'.$i;
								$res[]=replace_name($result,$arr);	
							}else{
								$arr[$rule['src']]=$rule['dst'].'_'.$i;
								$res[]=replace_name($result,$arr);	
							}
						}
						
					}else{
						$res[0]=$result;
						if($rule['dst']=='$$$0'){
							$arr[$rule['src']]=$fd;
						}else{
							$arr[$rule['src']]=$rule['dst'];
						}
						$res[0]=replace_name($res[0],$arr);	
					}
					
					$result=implode("\n",$res);
					break;	
					case 'callback':
						$result=call_user_func_array(array($callback_0,$ext),array($rule['src'],$rule['dst'],$result));
					break;
					case 'regexp':
						$result=preg_replace($rule['src'],$rule['dst'],$result);
					break;

				}
				
			}
		}
		$big[]=$result;
	}
	$result=stripslashes(implode("\n",$big));
}

isset($rules) or $rules=array();
isset($text) or $text='';

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>

<form method="post">
<table width="800" border="1">
  <tr>
    <th scope="row">规则</th>
    <td>
    <ul>
    <?php 
	$data=$db->mselect("select * from rules order by id asc");
	foreach($data as $k=>$v){
	?>
    <li><label><input type="checkbox" name="rules[]" value="<?php echo $v['id'];?>" <?php if(in_array($v['id'],$rules)) echo "checked";?>>将<?php echo $v['src'];?>替换成<?php echo $v['dst'];?>，类型<?php echo $v['kind'];?>，次数<?php echo $v['times'];?></label></li>
    <?php
	}
	?>
    </ul>
    </td>
  </tr>
  <tr>
    <th scope="row">字段</th>
    <td><textarea name="fields" cols="100" rows="10"><?php echo isset($fields)?$fields:'';?></textarea></td>
  </tr>
  <tr>
    <th scope="row">文本</th>
    <td>
    <select id="tpl">
    <option value="">====</option>
    <?php
    	foreach($files as $k=>$v){
			if($v=='.' || $v=='..') continue;
	?>
    	<option value="<?php echo './template/'.$v;?>"><?php echo $v;?></option>
	<?php
        }
    ?>
    </select>
    <textarea name="text" id="text" cols="100" rows="10"><?php echo isset($text)?$text:'';?></textarea><br />

	<input type="button" id="to_save" value="保存">
    </td>
  </tr>
  <tr>
    <th scope="row">结果</th>
    <td>
    <textarea name="result" id="result" cols="100" rows="10"><?php echo isset($result)?$result:'';?></textarea><br />
	<input type="button" id="to_reverse" value="设为文本">
	</td>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td align="center"><input type="submit" value="提交" name="submit" /></td>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td align="center"><a href="onevsone.php?text=<?php echo urlencode($result);?>">一对一替换</a></td>
  </tr>
</table>
</form>
<br />
<br />
<br />
<br />
<br />
<br />
<br />

<script src="jquery.js"></script>
<script>
$(document).ready(function(){
	$('#tpl').change(function(){
		if($(this).val()){
			$.post('get_content.php',{f:$(this).val()},function(data){
				$('#text').val(data);
			},'html');	
		}	
	});	
	$('#to_save').click(function(){
		if($('#text').val()){
			$.post('put_content.php',{f:$('#text').val()},function(data){
				$('#tpl').append("<option value='./template/"+data+"'>"+data+"</option>");
			},'html');	
		}	
	});
	$('#to_reverse').click(function(){
		$('#text').val($('#result').val());	
		$('#result').val('');
	});
});
</script>
</body>
</html>
